<?php
namespace Service;

class UserBank extends Base
{

    public function getDatabase()
    {
        return $this->get(DATABASE_USERBANK);
    }

    function hasAddByBankCode($userId, $bankCode)
    {
        $data = [
            'dry_user' => $userId,
            'dry_bank_code' => $bankCode
        ];
        return $this->getOneByFields($data);
    }

    public function addData($userId, $name, $idCard, $bankCard, $bankCode, $phone)
    {
        $coreEncrypt = $this->get(CORE_ENCRYPT)->setKey('sensitive');
        $idCard = $coreEncrypt->encrypt($idCard);
        $bankCard = $coreEncrypt->encrypt($bankCard);
        $phone = $coreEncrypt->encrypt($phone);
        $now = getDateTime();
        $insert = [
            'dry_user' => $userId,
            'dry_name' => $name,
            'dry_id_card' => $idCard,
            'dry_bank_card' => $bankCard,
            'dry_bank_code' => $bankCode,
            'dry_phone' => $phone,
            'dry_add_time' => $now['date_time']
        ];
        return $this->add($insert);
    }

    public function getList($userId, $page, $limit)
    {
        return $this->getDatabase()->getList($userId, $page, $limit);
    }

    public function decrypt($list = [])
    {
        $coreEncrypt = $this->get(CORE_ENCRYPT)->setKey('sensitive');
        foreach($list as $k => $v){
            $v->dry_id_card = $coreEncrypt->decrypt($v->dry_id_card);
            $v->dry_bank_card = $coreEncrypt->decrypt($v->dry_bank_card);
            $v->dry_phone = $coreEncrypt->decrypt($v->dry_phone);
            $list[$k] = $v;
        }
        return $list;
    }

}